var tab = $("#tab");
$(function () {
    query();
})
function query() {
    loading.show();
    $.ajax({
        url: "btgset/query.json",
        type: "post",
        data: {},
        dataType: "json",
        error: onajaxerror,
        success: function (result) {
            loading.hide();
            if (isSuccess(result)) {
                tab.bootstrapTable("load", result.data)
            } else {
                onAjaxRequestFailure(result);
            }
        }
    });
}
function initEvn() {
    confirmMsg("此操作会删除目前的所有配置信息，并加载默认的配置内容是否继续？", {
        yes: function () {
            loading.show();
            $.ajax({
                url: "btgset/initEvn.json",
                type: "post",
                data: {},
                dataType: "json",
                error: onajaxerror,
                success: function (result) {
                    loading.hide()
                    if (isSuccess(result)) {
                        alertMsg(result.msg)
                        query();
                    } else {
                        onAjaxRequestFailure(result);
                    }
                }
            });
        }
    });

}
function stateFormat(value, row) {
    var re = "";
    switch (value) {
        case "0":
            re = "<span style='color:green'>未使用</span>";
            break;
        case "1":
            re = "<span style='color:red'>正在使用</span>";
            break;
    }
    return re;
}
function copyDef() {
    var set_code = $("#set_code").val()
    var set_name = $("#set_name").val()
    var remark = $("#remark").val()

    if (set_code == "") {
        alertMsg("请输入配置代码！");
        return;
    }
    if (set_name == "") {
        alertMsg("请输入配置名称！");
        return;
    }
    loading.show();
    $.ajax({
        url: "btgset/copyDef.json",
        type: "post",
        data: {
            set_code: set_code,
            set_name: set_name,
            remark: remark
        },
        dataType: "json",
        error: onajaxerror,
        success: function (result) {
            loading.hide();
            if (isSuccess(result)) {
                $('#myModal').modal('hide')
                query();
            } else {
                onAjaxRequestFailure(result);
            }
        }
    });
}
function sub(row) {
    var key_id = "";
    if (row) {
        var rows = tab.bootstrapTable("getSelections");
        if (rows.length != 1) {
            alertMsg("请选择一行数据再进行操作", {
                icon: 5
            })
            return;
        }
        key_id = rows[0]['key_id'];

    }
    btg.open({
        title: "查询当前配置",
        max: true,
        data: {
            key_id: key_id
        },
        btn: ['保存', '关闭'],
        doSubmit: "save",
        url: "sub.html",
        isTop: true,
        ondestroy: function (re) {
        }
    });
}
function applySet() {
    var rows = tab.bootstrapTable("getSelections");
    if (rows.length != 1) {
        alertMsg("请选择一行数据再进行操作", {
            icon: 5
        })
        return;
    }
    var key_id = rows[0]['key_id']
    confirmMsg("是否应用当前选中项【" + rows[0]['set_name'] + "】的配置信息？", {
        yes: function () {
            loading.show();
            $.ajax({
                url: "btgset/apply.json",
                type: "post",
                data: {
                    key_id: key_id
                },
                dataType: "json",
                error: onajaxerror,
                success: function (result) {
                    loading.hide()
                    if (isSuccess(result)) {
                        alertMsg(result.msg)
                        query();
                    } else {
                        onAjaxRequestFailure(result);
                    }
                }
            });
        }
    });
}
function del() {
    var rows = tab.bootstrapTable("getSelections");
    if (rows.length != 1) {
        alertMsg("请选择一行数据再进行操作", {
            icon: 5
        })
        return;
    }
    if (rows[0]['state'] == 1) {
        alertMsg("正在使用的配置信息不可删除！", {
            icon: 5
        })
        return;
    }
    var key_id = rows[0]['key_id']
    confirmMsg("是否删除当前选中项【" + rows[0]['set_name'] + "】的配置信息？", {
        yes: function () {
            loading.show();
            $.ajax({
                url: "btgset/del.json",
                type: "post",
                data: {
                    key_id: key_id
                },
                dataType: "json",
                error: onajaxerror,
                success: function (result) {
                    loading.hide()
                    if (isSuccess(result)) {
                        alertMsg(result.msg)
                        query();
                    } else {
                        onAjaxRequestFailure(result);
                    }
                }
            });
        }
    });
}